package com.jtech.together.view.fragment;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jtech.together.R;
import com.jtech.together.mvp.contract.HomeContract;
import com.jtech.together.mvp.presenter.HomePresenter;
import com.jtech.together.util.ActivityJump;
import com.jtech.together.view.activity.AddTrendsActivity;
import com.jtech.together.view.adapter.HomePagerAdapter;
import com.jtech.together.view.fragment.base.BaseFragment;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 首页
 * Created by wuxubaiyang on 2017/5/10.
 */
public class HomeFragment extends BaseFragment implements HomeContract.View, ViewPager.OnPageChangeListener {
    @Bind(R.id.tablayout_home)
    TabLayout tabLayout;
    @Bind(R.id.viewpager_home)
    ViewPager viewPager;
    @Bind(R.id.fab_home)
    FloatingActionButton fabAdd;

    private HomeContract.Presenter presenter;

    @Override
    public View createView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
        return layoutInflater.inflate(R.layout.fragment_home, viewGroup, false);
    }

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new HomePresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        //设置viewpager适配器
        viewPager.setAdapter(
                new HomePagerAdapter(getChildFragmentManager(), getFragments()));
        //添加pager切换监听
        viewPager.addOnPageChangeListener(this);
        //设置viewpager加载单页
        viewPager.setOffscreenPageLimit(0);
        //绑定viewpager与tablayout
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    protected void loadData() {
    }

    /**
     * 获取pager页面集合
     *
     * @return
     */
    private List<BaseFragment> getFragments() {
        List<BaseFragment> baseFragments = new ArrayList<>();
        baseFragments.add(TrendsFragment.newInstance());
        baseFragments.add(RecommendUserFragment.newInstance());
        return baseFragments;
    }

    public static HomeFragment newInstance() {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * fab添加动态按钮
     */
    @OnClick(R.id.fab_home)
    public void onFabAddClick() {
        ActivityJump.build(getActivity(), AddTrendsActivity.class)
                .jump();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            fabAdd.show();
        } else {
            fabAdd.hide();
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
    }
}